<?php  
namespace Admin\Controller;
class FriendController extends AdminController 
{
  public function index(){
        $title       =   I('get.title');
        $map['status']  =   array('egt',0);
        if(is_numeric($title)){
            $map['id|title']=   array(intval($title),array('like','%'.$title.'%'),'_multi'=>true);
        }else{
            $map['title']    =   array('like', '%'.(string)$title.'%');
        }

        $list   = $this->lists('friend', $map);
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '友链';
        $this->display();
    }

   public function add(){

   	if (IS_GET) {
   	 	$this->display();
   	}else{

  	$_POST['pic'] =$this->upload('pic');
  	$friend=D('Friend');
  	$data =$friend->create();
  	if ($data) {
  		$friend->add($data);
  		
  		$this->success('添加友链成功',U('index'));
  		}else{
  			$this->error($friend->getError());
  		}
  	}
    }

  
   public function del(){
  	$this->display();
  }
  
   public function update(){
    $friend= D('Friend');

  	if(IS_GET){
     $res = $friend->find(I('get.id'));
     $this->assign('res',$res);
    $this->display();
    }else{
      if($_FILES['pic']['error']!=4){
        $_POST['pic'] =$this->upload('pic');
      }
      
      $data = $friend->create();
      if ($data) {
        $friend->where(['id'=>I('post.id')])->save($data);
        $this->success('编辑成功',U('index'));
      }else{
        $this->error($friend->getError());
      }
  }

  }



  protected function upload($inputName)
    {
        $upload           = new \Think\Upload(); // 实例化上传类
        $upload->maxSize  = 3145728; // 设置附件上传大小
        $upload->exts     = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        // 上传单个文件
        $info = $upload->uploadOne($_FILES[$inputName]);
        if (!$info) {
            // 上传错误提示错误信息
            $this->error($upload->getError());
        } else {
// 上传成功 获取上传文件信息
            return $info['savepath'] . $info['savename'];

        }
}

public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'hide':
                $this->forbid('Friend', $map, ['success'=>'隐藏成功！','error'=>'隐藏失败！']);
                break;
            case 'show':
                $this->resume('Friend', $map, ['success'=>'显示成功！','error'=>'显示失败！']);
                break;
             case 'del':
                D('Friend')->where($map)->delete();
                $this->success('删除成功');
                break;
            default:
                $this->error('参数非法');
        }
    }




}






?>